package com.aj.activity.adapter;

import java.util.HashMap;
import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ImageView;

import com.aj.activity.R;
import com.aj.databases.entry.PhotoInfo;
import com.aj.utils.PortraitLoaderUtil;

public class PersonalAlbumPhotoAdapter extends BaseAdapter {

	private Context context;
	private List<PhotoInfo> dataList;
	private HashMap<Integer, View> viewCache = new HashMap<Integer, View>();
	private boolean isManageFlag = false;
	private boolean isChecked = false;

	public PersonalAlbumPhotoAdapter(Context context, List<PhotoInfo> dataList) {
		this.context = context;
		this.dataList = dataList;
	}

	@Override
	public int getCount() {
		return dataList.size();
	}

	@Override
	public Object getItem(int position) {
		return dataList.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	class ViewHolder {
		ImageView photo_1;
		CheckBox checkbox_photo;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		convertView = viewCache.get(position);
		final PhotoInfo photoInfo = dataList.get(position);
		ViewHolder viewHoler = null;
		if (convertView == null) {
			convertView = LayoutInflater.from(context).inflate(
					R.layout.item_person_album_photo, null);
			viewHoler = new ViewHolder();
			viewHoler.photo_1 = (ImageView) convertView
					.findViewById(R.id.photo_1);
			viewHoler.checkbox_photo = (CheckBox) convertView
					.findViewById(R.id.checkbox_photo);
			convertView.setTag(viewHoler);
			PortraitLoaderUtil.loadSrc(context, viewHoler.photo_1, null,
					photoInfo.getPhoto(), R.drawable.nomal_photo,
					R.drawable.nomal_photo);
			viewCache.put(position, convertView);
		} else {
			viewHoler = (ViewHolder) convertView.getTag();
		}
		if (isManageFlag) {
			viewHoler.checkbox_photo.setVisibility(View.VISIBLE);
			viewHoler.checkbox_photo
					.setOnCheckedChangeListener(new OnCheckedChangeListener() {
						@Override
						public void onCheckedChanged(CompoundButton buttonView,
								boolean isChecked) {
							if (isChecked) {
								PersonalAlbumAdapter.choosePhotoList
										.add(photoInfo);
							} else {
								PersonalAlbumAdapter.choosePhotoList
										.remove(photoInfo);
							}
						}
					});
			viewHoler.checkbox_photo.setChecked(isChecked);
		} else {
			viewHoler.checkbox_photo.setVisibility(View.GONE);
		}
		return convertView;
	}

	public void setManageFlag(boolean isManage) {
		isManageFlag = isManage;
		notifyDataSetChanged();
	}

	public void setCheckFlag(boolean isCheck) {
		isChecked = isCheck;
		notifyDataSetChanged();
	}
}
